package pzl.kt.study.base

//操作符重载函数  operator set get   []
class Matrix2(var a: Int, var b: Int, var c: Int, var d: Int) {

    operator fun plus(m: Matrix2): Matrix2 {
        return Matrix2(m.a + a, m.b + b, m.c + c, m.d + d)
    }

    //按坐标取值
    operator fun get(x: Int, y: Int): Int {
        var point = Pair(x, y)
        return when (point) {
            Pair(0, 0) -> a
            Pair(0, 1) -> b
            Pair(1, 0) -> c
            Pair(1, 1) -> d
            else -> 0
        }
    }

    //按坐标存值
    operator fun set(x: Int, y: Int, value: Int) {
        var point = Pair(x, y)
        when (point) {
            Pair(0, 0) -> a = value
            Pair(0, 1) -> b = value
            Pair(1, 0) -> c = value
            Pair(1, 1) -> d = value
        }
    }

    override fun toString(): String {
        return "a = ${a} ,b = ${b} ,c = ${c} ,d = ${d}"
    }


}